/*     */ package com.zimbra.cs.util.yauth;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileReader;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.io.Writer;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FileTokenStore
/*     */   extends TokenStore
/*     */ {
/*     */   private final File file;
/*     */   private final Map<String, String> tokens;
/*  37 */   private static final Logger LOG = Logger.getLogger(FileTokenStore.class);
/*     */   
/*     */   public FileTokenStore(File file) throws IOException {
/*  40 */     this.file = file;
/*  41 */     this.tokens = new HashMap();
/*  42 */     loadTokens();
/*     */   }
/*     */   
/*     */   /* Error */
/*     */   public String getToken(String appId, String user)
/*     */   {
/*     */     // Byte code:
/*     */     //   0: aload_0
/*     */     //   1: dup
/*     */     //   2: astore_3
/*     */     //   3: monitorenter
/*     */     //   4: aload_0
/*     */     //   5: getfield 5	com/zimbra/cs/util/yauth/FileTokenStore:tokens	Ljava/util/Map;
/*     */     //   8: aload_1
/*     */     //   9: aload_2
/*     */     //   10: invokestatic 7	com/zimbra/cs/util/yauth/FileTokenStore:key	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
/*     */     //   13: invokeinterface 8 2 0
/*     */     //   18: checkcast 9	java/lang/String
/*     */     //   21: aload_3
/*     */     //   22: monitorexit
/*     */     //   23: areturn
/*     */     //   24: astore 4
/*     */     //   26: aload_3
/*     */     //   27: monitorexit
/*     */     //   28: aload 4
/*     */     //   30: athrow
/*     */     // Line number table:
/*     */     //   Java source line #46	-> byte code offset #0
/*     */     //   Java source line #47	-> byte code offset #4
/*     */     //   Java source line #48	-> byte code offset #24
/*     */     // Local variable table:
/*     */     //   start	length	slot	name	signature
/*     */     //   0	31	0	this	FileTokenStore
/*     */     //   0	31	1	appId	String
/*     */     //   0	31	2	user	String
/*     */     //   2	25	3	Ljava/lang/Object;	Object
/*     */     //   24	5	4	localObject1	Object
/*     */     // Exception table:
/*     */     //   from	to	target	type
/*     */     //   4	23	24	finally
/*     */     //   24	28	24	finally
/*     */   }
/*     */   
/*     */   protected void putToken(String appId, String user, String token)
/*     */   {
/*  52 */     synchronized (this) {
/*  53 */       this.tokens.put(key(appId, user), token);
/*  54 */       saveTokens();
/*     */     }
/*     */   }
/*     */   
/*     */   public void removeToken(String appId, String user) {
/*  59 */     synchronized (this) {
/*  60 */       this.tokens.remove(key(appId, user));
/*     */     }
/*     */   }
/*     */   
/*     */   public int size() {
/*  65 */     return this.tokens.size();
/*     */   }
/*     */   
/*     */   private void saveTokens() {
/*  69 */     debug("Saving yauth tokens to file '%s'", new Object[] { this.file });
/*  70 */     Writer w = null;
/*     */     try {
/*  72 */       w = new FileWriter(this.file);
/*  73 */       writeTokens(w); return;
/*     */     } catch (IOException e) {
/*  75 */       throw new IllegalStateException("Unable to save tokens", e);
/*     */     } finally {
/*  77 */       if (w != null) {
/*     */         try {
/*  79 */           w.close();
/*     */         } catch (IOException e) {
/*  81 */           debug("Error closing tokens file '" + this.file + "'", new Object[] { e });
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void writeTokens(Writer w) throws IOException {
/*  88 */     BufferedWriter bw = new BufferedWriter(w);
/*  89 */     for (Map.Entry<String, String> e : this.tokens.entrySet()) {
/*  90 */       bw.write((String)e.getKey());
/*  91 */       bw.write(32);
/*  92 */       bw.write((String)e.getValue());
/*  93 */       bw.newLine();
/*     */     }
/*  95 */     bw.flush();
/*     */   }
/*     */   
/*     */   private void loadTokens() throws IOException {
/*  99 */     Reader r = null;
/*     */     try {
/* 101 */       r = new FileReader(this.file);
/* 102 */       readTokens(r);
/* 103 */       debug("Loaded yauth tokens from file '%s'", new Object[] { this.file });
/*     */ 
/*     */     }
/*     */     catch (FileNotFoundException e) {}catch (IOException e)
/*     */     {
/* 108 */       debug("Deleting invalid tokens file ''%s'", new Object[] { this.file });
/* 109 */       r.close();
/* 110 */       this.file.delete();
/*     */     } finally {
/* 112 */       if (r != null) {
/* 113 */         r.close();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void readTokens(Reader r) throws IOException {
/* 119 */     BufferedReader br = new BufferedReader(r);
/*     */     String line;
/* 121 */     while ((line = br.readLine()) != null) {
/* 122 */       String[] parts = line.split(" ");
/* 123 */       if (parts.length != 3) {
/* 124 */         throw new IOException("Invalid token file");
/*     */       }
/*     */       
/*     */ 
/* 128 */       this.tokens.put(key(parts[0], parts[1]), parts[2]);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void debug(String fmt, Object... args) {
/* 133 */     LOG.debug(String.format(fmt, args));
/*     */   }
/*     */   
/*     */   private static String key(String appId, String user) {
/* 137 */     return appId + " " + user;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/yauth/FileTokenStore.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */